package com.java4.connection;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import java.sql.Connection;

/**
 * @author jingjintao
 * @date 2021/6/10 - 23:58
 **/
public class C3P0Test {
    //方式一：
    @Test
    public void testGetConnection() throws Exception{
        //获取c3p0数据库连接池
        final ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.cj.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true");
        cpds.setUser("root");
        cpds.setPassword("root");
        //通过设置相关的参数，对数据库连接池进行管理：
        //设置初始时数据库连接池中的连接数
        cpds.setInitialPoolSize(10);
        final Connection conn = cpds.getConnection();
        System.out.println(conn);
    }

    //方式二：使用配置文件
    @Test
    public void testGetConnection1() throws Exception{
        final ComboPooledDataSource cpds = new ComboPooledDataSource("hellc3p0");
        final Connection conn = cpds.getConnection();
        System.out.println(conn);
    }
}
